home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / test / test_extcall.py < prev    next >
Encoding:
Python Source  |  2000-04-10  |  2.6 KB  |  144 lines

  1. from UserList import UserList
  2.  
  3. def f(*a, **k):
  4.     print a, k
  5.  
  6. def g(x, *y, **z):
  7.     print x, y, z
  8.  
  9. def h(j=1, a=2, h=3):
  10.     print j, a, h
  11.  
  12. f()
  13. f(1)
  14. f(1, 2)
  15. f(1, 2, 3)
  16.  
  17. f(1, 2, 3, *(4, 5))
  18. f(1, 2, 3, *[4, 5])
  19. f(1, 2, 3, *UserList([4, 5]))
  20. f(1, 2, 3, **{'a':4, 'b':5})
  21. f(1, 2, 3, *(4, 5), **{'a':6, 'b':7})
  22. f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b':9})
  23.  
  24. try:
  25.     g()
  26. except TypeError, err:
  27.     print "TypeError:", err
  28. else:
  29.     print "should raise TypeError: not enough arguments; expected 1, got 0"
  30.     
  31. try:
  32.     g(*())
  33. except TypeError, err:
  34.     print "TypeError:", err
  35. else:
  36.     print "should raise TypeError: not enough arguments; expected 1, got 0"
  37.     
  38. try:
  39.     g(*(), **{})
  40. except TypeError, err:
  41.     print "TypeError:", err
  42. else:
  43.     print "should raise TypeError: not enough arguments; expected 1, got 0"
  44.     
  45. g(1)
  46. g(1, 2)
  47. g(1, 2, 3)
  48. g(1, 2, 3, *(4, 5))
  49. class Nothing: pass
  50. try:
  51.     g(*Nothing())
  52. except AttributeError, attr:
  53.     pass
  54. else:
  55.     print "should raise AttributeError: __len__"
  56.  
  57. class Nothing:
  58.     def __len__(self):
  59.         return 5
  60. try:
  61.     g(*Nothing())
  62. except AttributeError, attr:
  63.     pass
  64. else:
  65.     print "should raise AttributeError: __getitem__"
  66.     
  67. class Nothing:
  68.     def __len__(self):
  69.         return 5
  70.     def __getitem__(self, i):
  71.         if i < 3:
  72.             return i
  73.         else:
  74.             raise IndexError, i
  75. g(*Nothing())
  76.  
  77. # make sure the function call doesn't stomp on the dictionary?
  78. d = {'a': 1, 'b': 2, 'c': 3}
  79. d2 = d.copy()
  80. assert d == d2
  81. g(1, d=4, **d)
  82. print d
  83. print d2
  84. assert d == d2, "function call modified dictionary"
  85.  
  86. # what about willful misconduct?
  87. def saboteur(**kw):
  88.     kw['x'] = locals()
  89. d = {}
  90. saboteur(a=1, **d)
  91. assert d == {}
  92.         
  93. try:
  94.     g(1, 2, 3, **{'x':4, 'y':5})
  95. except TypeError, err:
  96.     print err
  97. else:
  98.     print "should raise TypeError: keyword parameter redefined"
  99.     
  100. try:
  101.     g(1, 2, 3, a=4, b=5, *(6, 7), **{'a':8, 'b':9})
  102. except TypeError, err:
  103.     print err
  104. else:
  105.     print "should raise TypeError: keyword parameter redefined"
  106.  
  107. try:
  108.     f(**{1:2})
  109. except TypeError, err:
  110.     print err
  111. else:
  112.     print "should raise TypeError: keywords must be strings"
  113.  
  114. try:
  115.     h(**{'e': 2})
  116. except TypeError, err:
  117.     print err
  118. else:
  119.     print "should raise TypeError: unexpected keyword argument: e"
  120.  
  121. try:
  122.     h(*h)
  123. except TypeError, err:
  124.     print err
  125. else:
  126.     print "should raise TypeError: * argument must be a tuple"
  127.  
  128. try:
  129.     h(**h)
  130. except TypeError, err:
  131.     print err
  132. else:
  133.     print "should raise TypeError: ** argument must be a dictionary"
  134.  
  135. def f2(*a, **b):
  136.     return a, b
  137.  
  138. d = {}
  139. for i in range(512):
  140.     key = 'k%d' % i
  141.     d[key] = i
  142. a, b = f2(1, *(2, 3), **d)
  143. print len(a), len(b), b == d
  144.